package bar.dark.lee.alarm.effect.typing;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import bar.dark.lee.alarm.R;
import bar.dark.lee.alarm.common.BDUtil;
import bar.dark.lee.alarm.effect.EffectActivity;
import bar.dark.lee.alarm.effect.EffectListActivity;
import bar.dark.lee.alarm.effect.question.QuestionResultDialog;

public class EffectTypingActivity extends EffectActivity {

	private int effectNumber;
	private boolean flagTestReal;
	
	private ViewGroup viewCount;
	private TextView textCount;
	
	private ViewGroup viewTyping;
	private TextView textTyping;
	private EditText editAnswer;
	private TextView textCorrect;
	private Button btnSend;
	
	private String result;
	private int correctCount = 3;
	
	private ArrayList<String> listTyping;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.effecttyping_activity);
		
		Intent intent = getIntent();
		effectNumber = intent.getIntExtra(EffectListActivity.EFFECT_PLAY, -1);
		flagTestReal = intent.getBooleanExtra(EffectActivity.FLAG_TEST_REAL, false);
		
		if (flagTestReal)
		{
			TextView leftButton = (TextView) findViewById(R.id.superLeftButton);
			leftButton.setVisibility(View.INVISIBLE);
		}
		
		if (effectNumber == -1)
		{
			
		}
		else
		{
			super.setTitle(effectNumber);
		}
		
		setFindView();
		setDataSetting();
		setClickListener();
		
		countdown();
	}
	
	@Override
	public void setFindView() {
		
		viewCount = (ViewGroup) findViewById(R.id.effectTyping_viewCount);
		textCount = (TextView) findViewById(R.id.effectTyping_textCount);
		
		viewTyping = (ViewGroup) findViewById(R.id.effectTyping_viewTyping);
		textTyping = (TextView) findViewById(R.id.effectTyping_textTyping);
		editAnswer = (EditText) findViewById(R.id.effectTyping_editAnswer);
		textCorrect = (TextView) findViewById(R.id.effectTyping_textCorrect);
		btnSend = (Button) findViewById(R.id.effectTyping_btnSend);
	}

	@Override
	public void setDataSetting() {
		
		listTyping = new ArrayList<String>();
		
		listTyping.add(getString(R.string.effecttyping_question1));
		listTyping.add(getString(R.string.effecttyping_question2));
		listTyping.add(getString(R.string.effecttyping_question3));
		listTyping.add(getString(R.string.effecttyping_question4));
		listTyping.add(getString(R.string.effecttyping_question5));
		listTyping.add(getString(R.string.effecttyping_question6));
		listTyping.add(getString(R.string.effecttyping_question7));
		listTyping.add(getString(R.string.effecttyping_question8));
		listTyping.add(getString(R.string.effecttyping_question9));
		listTyping.add(getString(R.string.effecttyping_question10));
		
	}

	@Override
	public void setClickListener() {
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String answer = editAnswer.getText().toString();
				if (answer.length() == 0)
				{
					BDUtil.showDialogOK(superContext, "", gets(R.string.effectquestion_errordialog), null);
				}
				else
				{
					if (answer.equalsIgnoreCase(result+""))
					{
						playResult(true);
						
						Handler handler = new Handler()
						{
							@Override
							public void handleMessage(Message msg) {
								super.handleMessage(msg);
								
								if (correctCount == 1)
								{
									finish();
								}
								else
								{
									editAnswer.setText("");
									correctCount--;
									
									countdown();
								}
							}
						};
						
						handler.sendEmptyMessageDelayed(0, 2 * 1000);
					}
					else
					{
						playResult(false);
						
						Handler handler = new Handler()
						{
							@Override
							public void handleMessage(Message msg) {
								super.handleMessage(msg);
								
								editAnswer.setText("");
								
								countdown();
							}
						};
						
						handler.sendEmptyMessageDelayed(0, 2 * 1000);
					}
				}
			}
		});
	}

	@Override
	protected void tipButton() {
	}

	private void countdown()
	{
		Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) 
				{
					case 1:
						viewTyping.setVisibility(View.INVISIBLE);
						viewCount.setVisibility(View.VISIBLE);
						textCount.setText("3");
						break;
					case 2:
						textCount.setText("2");
						break;
					case 3:
						textCount.setText("1");
						break;
					case 4:
						viewCount.setVisibility(View.INVISIBLE);
						viewTyping.setVisibility(View.VISIBLE);
						
						textTyping.setText(setQuestion());
						textCorrect.setText(correctCount+"");
						
						break;
					default:
						break;
				}
			}
		};
		
		for (int i=0; i<4; i++)
		{
			handler.sendEmptyMessageDelayed(i+1, i * 500);
		}
	}
	
	private String setQuestion()
	{
		int type = (int)(Math.random()*listTyping.size());
		
		String returnValue = "";

		returnValue = listTyping.get(type);
		
		result = returnValue;
		
		return returnValue;
	}
	
	private void playResult(boolean result)
	{
		Intent intent = new Intent(superContext, QuestionResultDialog.class);
		intent.putExtra(QuestionResultDialog.QUESTION_RESULT, result);
		startActivity(intent);
	}
}
